home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / dl / dl_findcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  4.2 KB  |  140 lines

  1. /***********************************************************
  2. Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /*
  25. ** dl_findcache - See if a cached binary is up-to-date
  26. */
  27.  
  28. #define _auxtemp _auxtemp5    /* Bug in ldfcn.h */
  29. #include <sys/types.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <aouthdr.h>
  33. #include <filehdr.h>
  34. #include <syms.h>
  35. #include <ar.h>
  36. #include <ldfcn.h>
  37. #include <stdlib.h>
  38.  
  39. #include "dl.h"
  40.  
  41. #ifdef DEBUG
  42. #define D(x) (x)
  43. #else
  44. #define D(x)
  45. #endif
  46.  
  47. #define NREMOVE 32
  48.  
  49. static char *toberemoved[NREMOVE];
  50. static char **toberemovedp = toberemoved;
  51.  
  52. static char cachebuf[1030];
  53.  
  54.  
  55. extern int unlink (const char *);
  56. extern char *strncpy (char *, const char *, size_t);
  57. extern int strncmp (const char *, const char *, size_t);
  58. extern char *strcat (char *, const char *);
  59. extern int access (const char *, int);
  60. extern int creat (const char *, mode_t);
  61. extern int close (int);
  62.  
  63. void
  64. dl_remove_tmp(void)
  65. {
  66.     D(printf("dl_remove_tmp: removing tempfiles\n"));
  67.     while ( --toberemovedp >= toberemoved ) {
  68.     D(printf("removing %s\n", *toberemovedp));
  69.     unlink(*toberemovedp);
  70.     }
  71. }
  72.  
  73. int
  74. dl_findcache(char *obj, int ostamp, char **cname)
  75. {
  76.     int stamp;
  77.     LDFILE *ldptr;
  78.     AOUTHDR aouth;
  79.     int fd;
  80.     char *tname;
  81.     
  82.     *cname = cachebuf;
  83.     strncpy(cachebuf, obj, 1024);
  84.     if ( strncmp(cachebuf+strlen(cachebuf)-2, ".o", 2) == 0)
  85.       cachebuf[strlen(cachebuf)-2] = 0;
  86.     strcat(cachebuf, ".ld");
  87.     if ( access(cachebuf, 0) == 0 && (ldptr=ldopen(cachebuf, 0)) != 0) {
  88.     D(printf("Found cached file\n"));
  89.     stamp = SYMHEADER(ldptr).vstamp;
  90.     if ( ldohseek(ldptr) == FAILURE ) {
  91.         D(printf("Cannot seek to a.out header on %s\n", cachebuf));
  92.         ldclose(ldptr);
  93.         goto bad;
  94.     }
  95.     if ( FREAD((char *)&aouth, 1, sizeof(aouth), ldptr) != sizeof(aouth) ) {
  96.         D(printf("Cannot read a.out header from %s\n", cachebuf));
  97.         ldclose(ldptr);
  98.         goto bad;
  99.     }
  100.     if ( ! dl_checkrange(aouth.text_start, aouth.tsize) ||
  101.          ! dl_checkrange(aouth.data_start, aouth.dsize+aouth.bsize) ) {
  102.         D(printf("Addresses in use: T %x %x, D %x %x\n", aouth.text_start,\
  103.              aouth.tsize, aouth.data_start, aouth.dsize+aouth.bsize));
  104.         ldclose(ldptr);
  105.         goto bad;
  106.     }
  107.     ldclose(ldptr);
  108.     if ( (unsigned short)stamp == (unsigned short)ostamp ) {
  109.         /* They match! The .ld file was created from the current file */
  110.         /*XXXX Should also check that addresses are OK */
  111.         D(printf("And timestamp ok\n"));
  112.         return 1;
  113.     }
  114.     D(printf("But wrong timestamp, wtd %x, got %x\n", ostamp, stamp));
  115.     }
  116. bad:
  117.     D(printf("No correct cached file, trying to create one\n"));
  118.     /* Doesn't exist yet, or incorrect stamp. See if we can create it. */
  119.     fd = creat(cachebuf, 0777);
  120.     if ( fd >= 0) {
  121.        D(printf("But can create here\n"));
  122.        close(fd);
  123.        unlink(cachebuf);
  124.        return 0;
  125.     }
  126.     D(printf("And can't create either\n"));
  127.     /* Nope, can't create. Fill in filename in /tmp */
  128.     tname = tempnam("/usr/tmp", "dl.");
  129.     if ( toberemovedp >= toberemoved+NREMOVE ) {
  130.       dl_error("Too many temporary load images created", 0);
  131.       return 0;
  132.     }
  133.     if ( toberemovedp == toberemoved )
  134.       atexit(dl_remove_tmp);
  135.     *toberemovedp++ = tname;
  136.     strcpy(cachebuf, tname);
  137.     return 0;
  138. }
  139.  
  140.